package edu.scau.divineplace.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;

/**
 * @Author: x1aolone
 * @Date: 2021/1/17 15:47
 * 文件存储工具类
 */
public class FileUtils {

    private FileUtils () {}

    public static final String BASE_PATH = "G:/DivinePlaceImage/";


    public static byte[] get (String fileName) {
        String absolutePath = BASE_PATH + fileName;
        byte[] bytes = FileUtil.readBytes(absolutePath);
        return bytes;
    }

    public static String add (byte[] bytes) {
        String fileName = null;
        String absolutePath = null;
        boolean fileExist = true;
        while (fileExist) {
            // 如果出现UUID重复，重新产生新的UUID，直到成功创建新文件为止
            fileName = IdUtil.randomUUID();
            absolutePath = BASE_PATH + fileName;
            fileExist = FileUtil.exist(absolutePath);
        }
        FileUtil.writeBytes(bytes, absolutePath);
        return fileName;
    }


}
